package com.ql.controller.yk;

import com.ql.pojo.user.DataGridView;
import com.ql.pojo.yk.Doctor;
import com.ql.pojo.yk.Ypcg;
import com.ql.pojo.yk.Ypcg2;
import com.ql.pojo.yk.YpcgVo;
import com.ql.service.yk.YpcgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class YpcgController {
    @Autowired
    private YpcgService ypcgService;
    @RequestMapping(value = {"/createYpcg"})
    @ResponseBody
    public Map<String,Object> createYpcg(Ypcg2 ypcg, HttpSession session){
        HashMap<String, Object> map = new HashMap<>();
        Doctor doctor = (Doctor) session.getAttribute("doctor");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date kssj=null;
        try {
            kssj = sdf.parse(ypcg.getCgKssj());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Ypcg ypcg1 = new Ypcg();
        ypcg1.setCgNo(ypcg.getCgNo());
        ypcg1.setCgKssj(kssj);
        ypcg1.setCgShzt(ypcg.getCgShzt());
        ypcg1.setCgCgdmc(ypcg.getCgCgdmc());
        ypcg1.setCgCgzt(ypcg.getCgCgzt());
        ypcg1.setCgShbz(ypcg.getCgShbz());
        if(null!=doctor){
            ypcg1.setCgCgr(doctor.getDocid());
        }
        ypcgService.addYpcg(ypcg1);
        map.put("resultCode",200);
        return  map;
    }


    @RequestMapping(value = {"/toCreateYpcg"})
    public String toCreateYpcg(){
        return "createYpcg";
    }

    @RequestMapping(value = {"/selectYpcg"})
    @ResponseBody
    public DataGridView selectYpcg(YpcgVo ypcgVo){

        return  ypcgService.findPage(ypcgVo);
    }

    @RequestMapping(value = {"/deleteById"})
    @ResponseBody
    public Map<String,Object> deleteById(String cgNo){
          ypcgService.deleteByNo(cgNo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg","删除成功");
        return map;

    }


    /**
     * 根据采购单号删除
     * @param cgNo
     * @return
     */
    @RequestMapping(value = {"/deleteYpcgByNo"})
    @ResponseBody
    public Map<String,Object> deleteYpcgByNo(String cgNo){
        ypcgService.deleteByNo(cgNo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg","删除成功");
        return map;
    }
    /**
     * 根据采购单ID批量删除
     * @param ids
     * @return
     */
    @RequestMapping(value = {"/batchDeleteYpcg"})
    @ResponseBody
    public Map<String,Object> batchDeleteYpcg(int [] ids){
        ypcgService.deleteByIds(ids);
        HashMap<String, Object> map = new HashMap<>();
        map.put("msg","删除成功");
        return map;

    }
}
